<?php


function yanzheng($uid, $token, $jm_str)
{
    /*
     * $uid = 用户id ,$token = 用户token ,$jm_str = 加密串
    */
    $arr = \think\Db::name('userhome')->where('id', $uid)->field('id,phone,token,is_suosi_switch,is_zhuxiao_switch')->find();
    if ($arr['token'] != $token) {
        return 0;           //token有误返回0
    }
    if ($arr['is_suosi_switch'] == 1) {
        return 0;
    }
    if ($arr['is_zhuxiao_switch'] == 1) {
        return 0;
    }
    $jiami = '369878'; //和app一致的加密串
    $str1 = md5(strtotime(date('Y-m-d H:00:00')) + $uid + $arr['phone'] + $jiami);
    $str2 = md5(strtotime(date('Y-m-d H:00:00')) + $uid + $arr['phone'] + $jiami - 3600);
    $str3 = md5(strtotime(date('Y-m-d H:00:00')) + $uid + $arr['phone'] + $jiami + 3600);
    if ($jm_str == $str1 || $jm_str == $str2 || $jm_str == $str3) {
        return 2;       //正常返回2
    } else {
        return 1;   //加密有误返回1
    }
}

function allupload($files)
{
    $str = '';
    #dump($files);die;
    foreach ($files as $file) {
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        if ($info) {
            $str .= '/uploads/' . date("Ymd", time()) . '/' . $info->getFilename() . ',';
        } else {
            echo $file->getError();
        }
    }
    $str = trim($str, ',');
    return $str;
}

//图片http去除
function replace_http($image)
{
    $images = explode(',', $image);
    $str = '';
    $url = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'];
    // dump($url);die;
    // $image = str_replace($url, '', $image);
    foreach ($images as $key => $value) {
        $str .= str_replace($url, '', $value) . ',';
    }
    $image = trim($str, ',');
    return $image;
}

//函数返回
function ajax_return($status, $test, $content)
{
    $msg['code'] = $status;
    $msg['msg'] = $test;
    $msg['time'] = time();
    $msg['content'] = $content;
    die(json_encode($msg));
}

//多张图片上传base64
function duo_image($images = '')
{
    $images = explode(',', $images);
    $info = array();
    $info['images'] = '';
    foreach ($images as $kk => $vv) {
        $info['images'] .= "/upload_img/" . base64_image_content('data:image/jpeg;base64,' . $vv, './upload_img/') . ',';
    }

    $img = substr($info['images'], 0, -1);
    return $img;
}

function allupload_name($files)
{
    $str = array();
    #dump($files);die;
    $a = 0;
    foreach ($files as $file) {
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        if ($info) {
            $wenjian_data = $info->getInfo();
            $str[$a]['file'] = '/uploads/' . date("Ymd", time()) . '/' . $info->getFilename();
            $str[$a]['name'] = $wenjian_data['name'];
            $a++;
        } else {
            echo $file->getError();
        }
    }
    return $str;
}

function sanwuoneupload($file)
{
    if ($file) {
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        if ($info) {
            $wenjian_data = $info->getInfo();
            // dump($wenjian_data);die;
            $data['original'] = $wenjian_data['name'];
            $data['size'] = $wenjian_data['size'];
            $data['state'] = 'success';
            $data['title'] = $info->getFilename();
            $data['type'] = $info->getExtension();
            $data['url'] = '/uploads/' . date("Ymd", time()) . '/' . $info->getFilename();
            return $data;
//                return '/uploads/'.$info->getSaveName();
            // return '/uploads/'.date("Ymd",time()).'/'.$info->getFilename();
        } else {
            return $file->getError();
        }
    }
}

function oneupload($file)
{
    if ($file) {
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        if ($info) {

//                return '/uploads/'.$info->getSaveName();
            return '/uploads/' . date("Ymd", time()) . '/' . $info->getFilename();
        } else {
            return $file->getError();
        }
    }
}

/**
 * @param $file
 * @return string
 * 上传文件，并保持源文件名字不覆盖
 */
function oneuploadyuan($file)
{
    if ($file) {
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/' . date("Ymd", time()), '', false, '1');
        if ($info) {
            $data = array(
                'code' => 200,
                'msg' => '/uploads/' . date("Ymd", time()) . '/' . $info->getFilename(),
                'name' => $info->getFilename(),
            );
            return $data;
        } else {
            $data = array(
                'code' => 299,
                'msg' => $file->getError(),
                'name' => '',
            );
            return $data;
        }
    }
}

/**
 * @param $arr
 * @param $pid
 * @param $step
 * @return array
 * 无限分类
 */
function GetTree($arr, $pid, $step)
{
    global $tree;
    foreach ($arr as $key => $val) {
        if ($val['pid'] == $pid) {
//                $flg = str_repeat('└―', $step);
            $flg = '';
            $val['name'] = $flg . $val['name'];
            $tree[] = $val;
            GetTree($arr, $val['id'], $step + 1);
        }
    }
    return $tree;
}

/**
 * 验证数据不能为空
 * @param unknown $validate 验证字段信息
 * @return multitype:number unknown string |boolean
 */
function EmptyValidate($validate, $Data)
{
    if (!isset($Data[$validate])) {
        return '请求参数缺少字段' . $validate;
    }
    if (empty($Data[$validate])) {
        if (is_numeric($Data[$validate])) {
            $orderInfo[$validate] = $Data[$validate];
            return;
        }
        return '请求字段' . $validate . '不能为空';
    } else {
        return;
    }
}

/**
 * base64编码转图片
 */
function base64_image_content($base64_image_content, $path)
{
    //匹配出图片的格式
    if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
        $type = $result[2];
        $new_file1 = $path;
        if (!file_exists($new_file1)) {
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($new_file1, 0700);
        }
        $name = date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
        $new_file = $name . ".{$type}";
        $new_file2 = $new_file1 . $new_file;
        if (file_put_contents($new_file2, base64_decode(str_replace($result[1], '', $base64_image_content)))) {
            return $new_file;
        } else {
            return 'false1';
        }
    } else {
        return 'false2';
    }
}

/*
     * 图片路径
     */
function path($pic)
{
    if (empty($pic)) {
        $pic = "";
    } else {
        $http = substr($pic, 0, 4);
        if ($http != 'http') {
            $pic = "https://oss.xmw1688.com" . $pic;
        }else{
            if(strstr($pic, 'https://oss.yuncangxiaodian.com')) {
                $image = substr($pic, 31);
                $pic = "https://oss.xmw1688.com" . $image;
            }
        }
    }
    return $pic;
}

/*
     * 物理图片路径
     */
function wlpath($pic)
{
    if (empty($pic)) {
        $pic = "";
    } else {
        $http = substr($pic, 0, 4);
        if ($http != 'http') {
            $pic = $_SERVER['DOCUMENT_ROOT'] . $pic;
        }

    }
    return $pic;

}

function doCurlPostRequest($url, $requestString)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $requestString);
    $return = curl_exec($ch);
    curl_close($ch);
    return $return;
}

//Get
function doCurlGetRequest($url, $timeout = 5)
{
    $con = curl_init((string)$url);
    curl_setopt($con, CURLOPT_HEADER, false);
    curl_setopt($con, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($con, CURLOPT_TIMEOUT, (int)$timeout);

    return curl_exec($con);
}


/**
 * @param $arr
 * @param $key
 * @return array
 * 二位数组根据某个键值去重
 */
function array_unset_tt($arr, $key)
{
    //建立一个目标数组
    $res = array();
    foreach ($arr as $value) {
        //查看有没有重复项
        if (isset($res[$value[$key]])) {
            unset($value[$key]);  //有：销毁
        } else {
            $res[$value[$key]] = $value;
        }
    }
    $res = array_values($res);
    return $res;
}

/**
 * @return string
 * 随机生成6为数字字母
 */
function createInvitecode($length = 6)

{

// 生成字母和数字组成的6位字符串

    $str = range('A', 'Z');

// 去除大写的O，以防止与0混淆 

    unset($str[array_search('O', $str)]);

    $arr = array_merge(range(0, 9), $str);

    shuffle($arr);

    $invitecode = '';

    $arr_len = count($arr);

    for ($i = 0; $i < $length; $i++) {

        $rand = mt_rand(0, $arr_len - 1);

        $invitecode .= $arr[$rand];

    }

    return $invitecode;

}

/*
* PHP去除Html所有标签、空格以及空白的方法
*/
function cutstr_html($string, $sublen = 0)
{
    $string = strip_tags($string);
    $string = trim($string);
    $string = preg_replace("/\t/", "", $string);
    $string = preg_replace("/\r\n/", "", $string);
    $string = preg_replace("/\r/", "", $string);
    $string = preg_replace("/\n/", "", $string);
    $string = preg_replace("/ /", "", $string);
    if (!empty($sublen)) {
        $string = mb_substr($string, 0, $sublen);
    }
    return trim($string);
}

// //快递查询
// function kuaidi($com='',$num=''){


//             $key = 'tFqHwagE2057';                        //客户授权key
//             $customer = 'BDFA523FC8DC5FE5CD69EA9E40AE2A53';//查询公司编号
//             $param = array (
//                 'com' => $com ,             //快递公司编码
//                 'num' => $num,     //快递单号
//                 'phone' => '',                //手机号
//                 'from' => '',                 //出发地城市
//                 'to' => '',                   //目的地城市
//                 'resultv2' => '1'             //开启行政区域解析
//             );

//             //请求参数
//             $post_data = array();
//             $post_data["customer"] = $customer;
//             $post_data["param"] = json_encode($param);
//             $sign = md5($post_data["param"].$key.$post_data["customer"]);
//             $post_data["sign"] = strtoupper($sign);

//             $url = 'http://poll.kuaidi100.com/poll/query.do';    //实时查询请求地址

//             $params = "";
//             foreach ($post_data as $k=>$v) {
//                 $params .= "$k=".urlencode($v)."&";              //默认UTF-8编码格式
//             }
//             $post_data = substr($params, 0, -1);
//         // echo '请求参数<br/>'.$post_data;
//                    //发送post请求
//             $ch = curl_init();
//             curl_setopt($ch, CURLOPT_POST, 1);
//             curl_setopt($ch, CURLOPT_HEADER, 0);
//             curl_setopt($ch, CURLOPT_URL, $url);
//             curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
//             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//             $result = curl_exec($ch);
//             $data = str_replace("\"", '"', $result );
//             $data = json_decode($data);
//             return $data;
//         // echo '<br/><br/>返回数据<br/>';
//             // dump($data);

// }
// /**
//  * 
//  * 把对象转成数组
//  * @param $object 要转的对象$object 
//  */
// function objectToArray($object){  

//     $result = array();  

//     $object = is_object($object) ? get_object_vars($object) : $object;  

//     foreach ($object as $key => $val) {  

//         $val = (is_object($val) || is_array($val)) ? objectToArray($val) : $val;  

//         $result[$key] = $val;  
//     }  

//     return $result;  
// }
function content_url($content)
{
    if (empty($content)) {
        $content = '';
    } else {
        $url = $_SERVER['HTTP_HOST'];
        $content = str_ireplace("src=\"/", "src=\"http://" . $url . "/", $content);
    }
    return $content;
}



